//
//  SEGExcellentStaffDetailCollectionViewCell.m
//  UIUIUI
//
//  Created by Clavis on 2018/12/11.
//  Copyright © 2018 Clavis. All rights reserved.
//

#import "SEGExcellentStaffDetailCollectionViewCell.h"

@interface SEGExcellentStaffDetailCollectionViewCell ()

@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *positionLabel;
@property (nonatomic, strong) UILabel *prizeNameLabel;
@property (nonatomic, strong) UILabel *prizeDescLabel;
@property (nonatomic, strong) UIView *infoView;
@property (nonatomic, strong) UIButton *likeButton;

@property (nonatomic, strong) UIVisualEffectView *visualView;

@end

@implementation SEGExcellentStaffDetailCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = kColor_W2;
    self.layer.cornerRadius = 16;
    
    BOOL isIphone5 = [UIScreen mainScreen].bounds.size.width == 320;
    BOOL isIphone6 = [UIScreen mainScreen].bounds.size.width == 375;
    
    self.headImageView = ({
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.clipsToBounds = YES;
        imageView.layer.cornerRadius = 5.f;
        imageView.layer.masksToBounds = YES;
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"headimg160")];
        [self addSubview:imageView];
        imageView;
    });
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(isIphone5 ? 25 : 50);
        make.centerX.mas_equalTo(0);
        make.width.height.mas_equalTo(70);
    }];
    
    self.nameLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_1;
        lable.textAlignment = NSTextAlignmentCenter;
        lable.font = kFont_16;
        lable;
    });
    
    // 最大宽度 = 屏幕宽度 - cell左右间距 - label左右间距
    CGFloat maxWidth = LYScreenWidth() - 80 - 40;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headImageView.mas_bottom).offset(10);
        make.centerX.mas_offset(0);
        make.width.mas_lessThanOrEqualTo(maxWidth);
    }];
    
    self.positionLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_3;
        lable.font = kFont_13;
        lable.textAlignment = NSTextAlignmentCenter;
        lable;
    });
    [self.positionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(5);
        make.centerX.mas_offset(0);
        make.width.mas_lessThanOrEqualTo(maxWidth);
    }];
    
    self.prizeNameLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = [UIFont boldSystemFontOfSize:22];
        lable.textAlignment = NSTextAlignmentCenter;
        lable;
    });
    [self.prizeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.positionLabel.mas_bottom).offset(LYIsiPhoneX() ? 25 : 20);
        make.centerX.mas_offset(0);
        make.width.mas_lessThanOrEqualTo(maxWidth);
    }];
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = kColor_T;
    lineView.layer.cornerRadius = 2;
    [self addSubview:lineView];
    [self sendSubviewToBack:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.prizeNameLabel);
        make.bottom.equalTo(self.prizeNameLabel).mas_offset(-1);
        make.centerX.equalTo(self.prizeNameLabel);
        make.height.mas_equalTo(7);
    }];
    
    self.prizeDescLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self addSubview:lable];
        lable.textColor = kColor_1;
        lable.font = kFont_13;
        lable.textAlignment = NSTextAlignmentCenter;
        lable.numberOfLines = 0;
        lable;
    });
    [self.prizeDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.equalTo(self.prizeNameLabel.mas_bottom).offset(LYIsiPhoneX() ? 25 : 15);
    }];
    
    self.likeButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"" forState:UIControlStateNormal];
        [button setTitleColor:kColor_1 forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_best_staff")] forState:UIControlStateNormal];
        button.titleLabel.font = kFont_16;
        button.contentEdgeInsets = UIEdgeInsetsMake(-13, 0, 0, 0);
        [button addTarget:self action:@selector(likeButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        button;
    });
    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(161);
        make.height.mas_equalTo(59);
        make.centerX.offset(0);
        make.bottom.mas_equalTo(isIphone5 ? -8 : isIphone6 ? -33 : -44);
    }];
    
    self.infoView = [UIView new];
    [self addSubview:self.infoView];
    [self.infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        CGFloat offset = LYIsiPhoneX() ? -40 : (isIphone5 ? -8 : (isIphone6 ? -32 : -45));
        make.bottom.equalTo(self.likeButton.mas_top).offset(offset);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(36);
    }];
    
    // 添加阴影
    self.layer.shadowColor = [UIColor.blackColor colorWithAlphaComponent:0.7].CGColor;
    self.layer.shadowOffset = CGSizeMake(4, 4);
    self.layer.shadowOpacity = 0.7;
    self.layer.shadowRadius = 8;
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.bounds];
    //设置阴影路径
    self.layer.shadowPath = shadowPath.CGPath;
    
    //设置UIVisualEffectView
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    self.visualView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    self.visualView.userInteractionEnabled = NO;
    self.visualView.frame = self.bounds;
    self.visualView.alpha = 0;
    [self addSubview:self.visualView];
}

- (void)updateUIWithModel:(SEGExcellentStaffModel *)model {
    [self.headImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,model.empeePhoto ?: @""]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg160")]];
    self.nameLabel.text = model.empeeName;
    self.positionLabel.text = model.empeeJob;
    self.prizeNameLabel.text = model.prizeName;
    self.prizeDescLabel.text = model.prizeDesc;
    NSString *likeButtonTitle = [NSString stringWithFormat:@"%@%@", model.likeStatus == 0 ? @"已赞" : @"赞", [self formatLikeNumber:model.likeNum]];
    [self.likeButton setTitle:likeButtonTitle forState:UIControlStateNormal];
    
    // 布局员工扩展信息字段
    [self.infoView removeAllSubviews];
    NSInteger displayCount = model.extensionArray.count;
    CGFloat cellWidth = LYScreenWidth() - (15 + 20) * 2;
    [model.extensionArray enumerateObjectsUsingBlock:^(SEGExcellentStaffExtensionInfoModel * _Nonnull extensionModel, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *valueLabel = ({
            UILabel *lable = [[UILabel alloc] init];
            [self.infoView addSubview:lable];
            lable.textColor = kColor_1;
            lable.font = [UIFont boldSystemFontOfSize:16];
            lable.text = extensionModel.val;
            lable;
        });
        UILabel *titleLabel =  ({
            UILabel *lable = [[UILabel alloc] init];
            [self.infoView addSubview:lable];
            lable.textColor = kColor_3;
            lable.font = kFont_11;
            lable.text = extensionModel.name;
            lable;
        });
        [valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            NSInteger i = (NSInteger)idx;
            CGFloat offset = displayCount == 1 ? 0 : ( (displayCount == 2 ? (i-1)*140+70 : (i-1)*floor(cellWidth/3.0) ));
            make.centerX.offset(offset);
        }];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(0);
            make.centerX.equalTo(valueLabel.mas_centerX);
        }];
    }];
    
    if (displayCount == 3) {
        UIView *lineView = [UIView new];
        lineView.backgroundColor = kColor_L2;
        [self.infoView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(floor(cellWidth/6.0)*-1);
            make.centerY.mas_equalTo(0);
            make.height.mas_equalTo(24);
            make.width.mas_equalTo(1);
        }];
        
        UIView *line2View = [UIView new];
        line2View.backgroundColor = kColor_L2;
        [self.infoView addSubview:line2View];
        [line2View mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(floor(cellWidth/6.0));
            make.centerY.mas_equalTo(0);
            make.height.mas_equalTo(24);
            make.width.mas_equalTo(1);
        }];
    }
    
    if (displayCount == 2) {
        UIView *lineView = [UIView new];
        lineView.backgroundColor = kColor_L2;
        [self.infoView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.centerY.mas_equalTo(0);
            make.height.mas_equalTo(24);
            make.width.mas_equalTo(1);
        }];
    }
}

- (NSString *)formatLikeNumber:(NSInteger)likeNumber {
    if (likeNumber == 0) {
        return @"";
    }
    if (likeNumber >= 1000) {
        return [NSString stringWithFormat:@" (%.1lfk)", likeNumber/1000.0];
    }
    return [NSString stringWithFormat:@" (%ld)", (long)likeNumber];
}


- (void)likeButtonTap:(UIButton *)sender {
    if (self.faverTapBlock) {
        self.faverTapBlock();
    }
}

@end
